<?php
include_once "Security.class.php";
include_once 'Grid.class.php';

class BasePage {

    public $Ids;
    public $IdCount = 0; //everything beyond this is form value.
    protected $grid;
    private $GET;
    private $redirect = "";
    private $permissions = "";

    public function __construct($ids, $formVals, $get) {
        $this->buildIdsObj($ids, $formVals, $get);
        
    }
    
    public function Page_Load()
    {
        // Override this
    }
    
    /**
     *  Will determine whether or not user has access to page. If not, it will also set the message that will be sent to user which will
     *  trigger a redirect back to the index.
     * @return boolean Whether or not user has access to page 
     */
    public function CheckSecurity()
    {
        $security = new Security(get_class($this));
        $this->redirect = $this->permissions = $security->CheckSecurity();
        return $this->redirect == "";
    }

    public function initialize($ids, $formVals, $get) {
        $this->buildIdsObj($ids, $formVals, $get);
    }
    private function setGet($get)
    {
        if(count($get) > 0)
        {
            foreach($get as $key=>$val)
            {
                $this->GET[$key] = $val;
            }
        }
    }
    public function Get($key)
    {
        return $this->GET[$key];
    }
    protected function buildIdsObj($ids, $formVals, $get) {
        $this->setGet($get);
        $this->idCount=0;
        if(count($ids) > 0)
        {
            foreach ($ids as $id) {
                $this->Ids[$id["id"]] = $id;  //set ids equal to id. This way, on controller's end, only set if not the same
                $this->$id["id"]->id = $id["id"];   //do this so can more easily manipulate/access variables in the dependant classes
                $this->$id["id"]->html = $id["html"];
                $this->$id["id"]->val = $id["val"];
                $this->$id["id"]->addClass = $id["addClass"];
                $this->$id["id"]->removeClass = $id["removeClass"];
                $this->idCount++;
            }
        }
        //Get all the variables from forms
        if (count($formVals) > 0) {
            foreach ($formVals as $key => $val) {
                $this->Ids[$key] = $val;
                $this->$key->html = $val["html"];  //do this so can more easily manipulate/access variables in the dependant classes
                $this->$key->val = $val["val"];
                $this->$key->addClass = $val["addClass"];
                $this->$key->removeClass = $val["removeClass"];
            }
        }
        $this->generateDynamicJS();
    }

    public function EncodeIds() {
        //Get all of the Ids back into the Ids array for easy Encoding.
        if(count($this->Ids) > 0)
        {
            foreach ($this->Ids as $key => $val) {
                $this->Ids[$key]["id"] = $this->$key->id;
                $this->Ids[$key]["html"] = $this->$key->html;
                $this->Ids[$key]["val"] = $this->$key->val;
                $this->Ids[$key]["addClass"] = $this->$key->addClass;
                $this->Ids[$key]["removeClass"] = $this->$key->removeClass;
            }
        }
        $ids = array();
        $i = 0;
        if(count($this->Ids)>0)
        {
            foreach ($this->Ids as $key => $val) {
                $ids[$key]["id"] = $val["id"];
                $ids[$key]["html"] = $val["html"];
                $ids[$key]["val"] = $val["val"];
                $ids[$key]["addClass"] = $val["addClass"];
                $ids[$key]["removeClass"] = $val["removeClass"];
                $i++;
                if ($i == $this->idCount)
                    break;
            }
        }
        $ar["ids"] = $ids;
        $ar["form"] = count($this->Ids) > 0 ? array_diff($this->Ids, $ids) : array();
        $ar["redirect"] = $this->redirect;
        $ar["permissions"] = $this->permissions;

        return json_encode($ar);
    }
    
    
    private function getClassName() {
        $className = get_class($this);
        $className{0} = strtolower($className{0});
        return $className;
    }
    private function isRestrictedMethod($method)
    {
        $restrictedMethods = array("Page_Load","__construct","CheckSecurity","initialize","EncodeIds");
        return in_array($method,$restrictedMethods);
    }
    
    protected function Redirect($url) {
        $this->redirect = $url;
    }

    protected function ArrayToTable($ar) {
        $table = "<table border='1'>";
        for ($i = 0; $i < count($ar); $i++)
        {
            if($i==0)
                $table.="<tr>" . $this->getTableHeader($ar[$i]) . "</tr>";
           $table.="<tr>" . $this->getTableRow($ar[$i]) . "</tr>";
        }
        $table.="</table>";
        return $table;
    }
    protected function getTableHeader($row)
    {
        $rowHtml = "";
        $columns = array_keys($row);
        foreach ($columns as $col) {
            
            $rowHtml.="<th>$col</th>";
        }
        return $rowHtml;
    }
    protected function getTableRow($row) {
        $rowHtml = "";
        $columns = array_keys($row);
        foreach ($columns as $col) {
            
            $rowHtml.="<td>" . $row[$col] . "</td>";
        }
        return $rowHtml;
    }
    
    private function generateDynamicJS()
    {
        $js = "";
        $className = $this->getClassName();
        $modelReflector = new ReflectionClass($className);
        $methods = $modelReflector->getMethods(ReflectionMethod::IS_PUBLIC);
        
        if(count($methods) > 0)
        {
            foreach($methods as $method)
            {
                $methodName = $method->name;
                
                if($this->isRestrictedMethod($methodName) === false)
                {
                    $parameters = $method->getParameters();
                    $parameterString = "";
                    $arrayString = "PARAMETERS = new Array();";
                    if(count($parameters) > 0)
                    {
                        foreach($parameters as $p)
                        {
                            $parameterString .= $p->name.",";
                            $arrayString.="
PARAMETERS.push(".$p->name.");";
                        }
                        $parameterString = substr($parameterString, 0,-1);
                    }
                    
                $js.="
function $methodName($parameterString){
    $arrayString
    action('$methodName');
}
";
                }
            }
        }
        $js.="";
        $fp = fopen("dynamicJS.js","w");
        fwrite($fp, $js);
        fclose($fp);
    }
}

?>